package com.ellricky.baselib.adapter;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andr.common.ui.adapter.BaseRecyclerAdapter;
import com.andr.common.ui.adapter.SmartViewHolder;
import com.andr.common.util.EmptyUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.entity.CheckProgressItemEntity;
import com.ellricky.baselib.listener.OnDoProgressScoreListener;
import com.ellricky.baselib.listener.OnTakePhotoListener;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.util.EDialogUtil;
import com.flyco.roundview.RoundTextView;

public class CheckProgressAdapter extends BaseRecyclerAdapter<CheckProgressItemEntity> {

    private Activity ctx;
    // yes =是负责人 no =不是负责人
    private boolean isMyself;
    private OnDoProgressScoreListener listener;

    public CheckProgressAdapter(Activity ctx, OnDoProgressScoreListener listener) {
        super(R.layout.base_adapter_check_progress);
        this.ctx = ctx;
        this.listener = listener;
    }

    public void setMyself(boolean myself) {
        isMyself = myself;
    }

    @Override
    public void onBindViewHolder(SmartViewHolder holder, final CheckProgressItemEntity model, int position) {

        holder.text(R.id.tvName, model.getName());

        RelativeLayout rlFile = holder.itemView.findViewById(R.id.rlFile);
        TextView tvSee = holder.itemView.findViewById(R.id.tvSee);

        final String picUrl = model.getPicUrl();
        if (EmptyUtil.isEmpty(picUrl)) {
            rlFile.setVisibility(View.INVISIBLE);
            tvSee.setOnClickListener(null);
        } else {
            rlFile.setVisibility(View.VISIBLE);
            tvSee.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EAppUtil.startFileViewerPage(ctx, picUrl);
                }
            });
        }

        // 参加状态
        boolean isAttend = "2".equals(model.getIsAttend());
        TextView tvAttendState = holder.itemView.findViewById(R.id.tvAttendState);
        if (isAttend) {
            tvAttendState.setText("已参加");
        } else {
            tvAttendState.setText("未参加");
        }

        // 奖励文本
        TextView tvCheckState = holder.itemView.findViewById(R.id.tvCheckState);
        RoundTextView tvDeal = holder.itemView.findViewById(R.id.btnDeal);
        String isGive = model.getIsGive();
        // 是负责人&已参与&未评价
        if (isMyself && isAttend && "1".equals(isGive)) {
            tvDeal.setVisibility(View.VISIBLE);
            tvDeal.setText("处理");
            tvCheckState.setVisibility(View.GONE);
        } else {
            tvDeal.setVisibility(View.GONE);
            tvCheckState.setVisibility(View.VISIBLE);
            tvCheckState.setText(model.getIsGiveName());
        }

        tvDeal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EDialogUtil.showScoreDialog(ctx, new OnTakePhotoListener() {
                    @Override
                    public void onClickItem(DialogInterface dialogInterface, int item) {
                        dialogInterface.dismiss();
                        if (listener != null) {
                            String isGive = item == 0 ? "2" : item == 1 ? "3" : "";
                            listener.doScore(model.getId(), isGive);
                        }
                    }
                });
            }
        });

    }
}


